#include "patient.h"
#include "ui_patient.h"

patient::patient(QWidget *parent,const QString name_V,const QString pass_V) :
    QMainWindow(parent),
    ui(new Ui::patient)
{
    name_text=name_V;
    passwd_text=pass_V;
    ui->setupUi(this);
    QFont font("Microsoft YaHei",12,75);
    QFont font_line("宋体",12,30);


    this->setWindowTitle(tr("病患显示界面"));


    date=new QDateEdit(QDate::currentDate() ,this);//表示默认为当天
    date->setMinimumDate(QDate::currentDate().addDays(-30));//表示显示有效期最早为之前30天。
    date->setMaximumDate(QDate::currentDate());//表示显示有效期最晚为今天。
    date->setCalendarPopup(true);//以日历状态展示
    date->setGeometry(10,10,150,30);
    date->setFont(font);

    name=new QLabel(this);
    name->setGeometry(160,10,100,30);
    name->setText(QObject::tr("姓名:"));
    name->setFont(font);
    name->setAlignment(Qt::AlignJustify|Qt::AlignCenter);


    name_value=new QLabel(this);
    name_value->setText(name_V);
    name_value->setGeometry(260,10,100,30);
    name_value->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    name_value->setFont(font);

    Number=new QLabel(this);
    Number->setText(QObject::tr("病床号:"));
    Number->setGeometry(360,10,100,30);
    Number->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    Number->setFont(font);

    Number_value=new QLabel(this);
    Number_value->setText(QObject::tr("xxx-xxx-xx"));
    Number_value->setGeometry(460,10,100,30);
    Number_value->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    Number_value->setFont(font);
//####################
//创建显示氧气使用量那一行
//###################
    use_v=new QLabel(this);
    use_v->setText(QObject::tr("氧 气 使 用 量 :"));
    use_v->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    use_v->setGeometry(80,80,150,30);
    use_v->setFont(font);
    use_line=new QLabel(this);
    use_line->setGeometry(250,80,170,30);
    use_line->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    use_line->setFont(font_line);
//####################
//创建显示费用那一行
//###################
    money=new QLabel(this);
    money->setText(QObject::tr("费用 :"));
    money->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    money->setGeometry(80,150,150,30);
    money->setFont(font);
    money_line=new QLabel(this);
    money_line->setGeometry(250,150,170,30);
    money_line->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    money_line->setFont(font_line);

    money_unit=new QLabel(this);
    money_unit->setText(QObject::tr("元"));
    money_unit->setGeometry(420,150,30,30);
    money_unit->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    money_unit->setFont(font_line);
//####################
//创建显示总费用那一行
//###################
    A_money=new QLabel(this);
    A_money->setText(QObject::tr("总费用 :"));
    A_money->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    A_money->setGeometry(80,220,150,30);
    A_money->setFont(font);
    A_money_line=new QLabel(this);
    A_money_line->setGeometry(250,220,170,30);
    A_money_line->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    A_money_line->setFont(font_line);

    A_money_unit=new QLabel(this);
    A_money_unit->setText(QObject::tr("元"));
    A_money_unit->setGeometry(420,220,30,30);
    A_money_unit->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    A_money_unit->setFont(font_line);
//####################
//创建显示按钮那一行
//###################
    Warning_ptn=new QPushButton(this);
    Warning_ptn->setText(QObject::tr("设备异常报警"));
    Warning_ptn->setGeometry(125,280,100,30);
    exit_ptn=new QPushButton(this);
    exit_ptn->setText(QObject::tr("退出"));
    exit_ptn->setGeometry(400,280,100,30);
    this->setFixedSize(600,400);//设置窗口大小为固定大小
//####################
//状态栏信息
//###################
    message_left=new QLabel(this);
    message_left->setAlignment(Qt::AlignCenter);
    message_left->setFrameShape(QFrame::WinPanel);
    message_left->setFrameShadow(QFrame::Sunken);
    message_left->setText(QObject::tr("医疗氧气智能监控系统V1.0"));
    ui->statusbar->addWidget(message_left);

    message_right=new QLabel(this);
    message_right->setFrameStyle(QFrame::Box|QFrame::Sunken);
    message_right->setText(QObject::tr("测试版"));
    //message_right->setTextFormat(Qt::RichText);
   // message_right->setOpenExternalLinks(true);
    message_right->setMinimumSize(80,20);
    ui->statusbar->addPermanentWidget(message_right);
    DATA_Display();
    connect(Warning_ptn,&QPushButton::clicked,this,&patient::Warning_ptn_clicked);
    connect(exit_ptn,&QPushButton::clicked,this,&patient::exit_ptn_clicked);


}

patient::~patient()
{
    delete ui;
}

int patient::DATA_Display()//将数据从数据库中显示在桌面，并计算出费用和总费用以及使用量
{
    if(!CON_MYSQL_Patient())
    {
        QMessageBox::warning(NULL,"CONNECT DATABASE ERROR","连接数据库失败",QMessageBox::Ok);
        return 0;
    }
    query=QSqlQuery(db);//对QSqlQuery::exec: database not open错误的解决方案,注意：：：一定要在连接之后才能调用，因为连接数据库之后，db1才是连接好的。
    if(!query.exec("select * from Patient_Login_Hospital"))
    {
        qDebug()<<"搜索出错"<<query.lastError();
        QMessageBox::warning(NULL,"SOURCH CLOUMNS ERROR","搜索出错",QMessageBox::Ok);
        return 0;
    }
    while(query.next())
    {
        qDebug()<<"DDDDD";

        if( name_text==query.value(3).toString()&&passwd_text==query.value(7).toString())
        {
             room_num=query.value(8).toString();
            name_value->setText(name_text);
            Number_value->setText(room_num);

            qDebug()<<"TTTTTTTTTTTT"<<name_text<<passwd_text;
            return 1;
        }

    } //设置姓名和床号

       // QMessageBox::warning(NULL,"INCROOECT","用户名或密码或账户类型不正确，请重新输入",QMessageBox::Ok);
        return 0;
}

bool patient::CON_MYSQL_Patient()
{
    db=QSqlDatabase::addDatabase("QMYSQL","CON_Patient");
     qDebug() << "MYSQL driver valid" << db.isValid();
              db.setHostName("2001:da8:270:2018:f816:3eff:fe1d:bb01");
              db.setPort(3306);
              db.setDatabaseName("MQTT_DATA");
              db.setUserName("root");
              db.setPassword("lama940225");
              if(!db.open())
              {
                  qDebug()<<"打开数据库失败！"<<db.lastError();
                  return false;
              }

              return true;
}

void patient::exit_ptn_clicked()
{
    db.close();
    QString CON_NAME=db.connectionName();
    db.removeDatabase(CON_NAME);
    this->close();
}

void patient::Warning_ptn_clicked()
{
    QMessageBox::warning(this,tr("警告"),tr("已警告"),QMessageBox::Yes,QMessageBox::Close);
}
